 #include<iostream>
#include<string>
#include"contacts.pb.h"
#include<fstream>
using namespace std;
void AddPeopleInfo(s_contacts::PeopleInfo* pcont)
{
    string name;
    cout<<"-----新增联系人------"<<endl;
    cout<<"请输入姓名：";
    getline(cin,name);
    pcont->set_name(name);
    cout<<"请输入生日：";
    int age;
    cin>>age;
    pcont->set_birthed(age);
    cin.ignore(256,'\n');
    for(int i =0;;i++)
    {
        cout<<"请输入联系电话"<<i+1<<":(只输入回车完成新增)";
        string number;
        getline(cin,number);
        if(number.empty())
        {
            break;
        }
        s_contacts::PeopleInfo_Phone* phone = pcont->add_phione();
        phone->set_number(number);
         cout<<"请输入电话类型(1.移动电话 2.固定电话):";
        int type;
        cin>>type;
        cin.ignore(256,'\n');
        switch(type)
        {
            case 1:
                phone->set_type(s_contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(s_contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout<<"输入有误"<<endl;
                break;    
        }

    }
    cout<<"输入成功"<<endl;
}

int main()
{
    fstream input("test.bin",ios::in|ios::binary);
    s_contacts::PeopleContacts conts;
    if(!input)
    {
        cerr<<"file not find,creat new file"<<endl;
    }
    else if(!conts.ParseFromIstream(&input))
    {
        cerr<<"failed to parse contacts"<<endl;
        input.close();
        return -1;
    }
    AddPeopleInfo(conts.add_cont());
    fstream output("test.bin",ios::out|ios::trunc|ios::binary);
    if(!conts.SerializeToOstream(&output))
    {
        cerr<< "filed to write contacts"<<endl;
        input.close();
        output.close();
        return -1;
    }
    input.close();
    output.close();
    return 0;
}